import VueRouter from 'vue-router'
import Vue from 'vue'
import { Message } from 'element-ui';
// vue.use(VueRouter)
//引入组件
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Goods from '@/pages/GoodsList'
import User from '@/pages/UserInfo'
import Order from '@/pages/Order'
import Orders from '@/pages/Orders'
import UserList from '@/pages/Users.vue'
import Pay from '@/pages/pay'
import stock from "@/pages/stock";
import shoppingCart from "@/pages/shoppingCart";
import dataAnalysis from "@/pages/dataAnalysis";
import Add from "@/pages/Add";

Vue.use(VueRouter)
//创建并暴露一个路由器
console.log('进来了')
export default new VueRouter({

    routes: [
        {
            path: '/login',
            component: Login
        },
        {
            path: '/regist',
            component: Register
        },
        {
            //path: '/goods',
            path:'/',
            component: Goods
        },
        {
            path: '/userInfo',
            component: User,
            meta:{isAuth:true,title:'个人订单'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            //alert('学校名字不对无法查看')
                            next()
                    }
                  }else{
                        next()
                 }  
            }
        },
        {
            path: '/order',
            component: Order,
            meta:{isAuth:true,title:'个人订单'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            //alert('学校名字不对无法查看')
                            next()
                    }
                  }else{
                        next()
                 }  
            }
        },
        {
            path: '/orderList',
            component: Orders,
            meta:{isAuth:true,title:'订单列表'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            if(JSON.parse(sessionStorage.getItem('user')).status){
                                console.log(JSON.parse(sessionStorage.getItem('user')).status+"=")
                                Message.error("您没有权限！")
                            }else{
                                next()
                            }
                            
                    }
                  }
                  else{
                        next()
                    }
            }

        }, {
            path: '/userList',
            component: UserList,
            meta:{isAuth:true,title:'用户列表'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            if(JSON.parse(sessionStorage.getItem('user')).status){
                                console.log(JSON.parse(sessionStorage.getItem('user')).status+"=")
                                Message.error("您没有权限！")
                            }else{
                                next()
                            }
                            
                    }
                  }
                  else{
                        next()
                    }
            }
        },
        {
            path: '/pay',
            component: Pay
        }, {
            path: '/stock',
            component: stock,
            meta:{isAuth:true,title:'库存'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            if(JSON.parse(sessionStorage.getItem('user')).status){
                                console.log(JSON.parse(sessionStorage.getItem('user')).status+"=")
                                Message.error("您没有权限！")
                            }else{
                                next()
                            }
                            
                    }
                  }
                  else{
                        next()
                    }
            }
        },
        {
            path: '/shoppingCart',
            component: shoppingCart,
            meta:{isAuth:true,title:'购物车'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            next()
                    }
                  }else{
                        next()
                 }  
            }
        },
        {
            path: '/dataAnalysis',
            component: dataAnalysis,
            meta:{isAuth:true,title:'数据分析'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            if(JSON.parse(sessionStorage.getItem('user')).status){
                                console.log(JSON.parse(sessionStorage.getItem('user')).status+"=")
                                Message.error("您没有权限！")
                            }else{
                                next()
                            }
                    }
                  }
                  else{
                        next()
                    }
            }
        },{
            path: '/Add',
            component: Add,
            meta:{isAuth:true,title:'添加商品'},
            beforeEnter:(to,from,next)=>{
                if(to.meta.isAuth){ //判断是否需要鉴权
                    if(sessionStorage.getItem('user')===null){
                        Message.error("请登录！")
                    }else{
                            if(JSON.parse(sessionStorage.getItem('user')).status){
                                console.log(JSON.parse(sessionStorage.getItem('user')).status+"=")
                                Message.error("您没有权限！")
                            }else{
                                next()
                            }
                    }
                  }
                  else{
                        next()
                    }
            }
        }
    ]
})